package com.yuhanginfo.modules.web.web;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.utils.FileUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;

import com.yuhanginfo.modules.web.entity.WebEnquiryGoods;
import com.yuhanginfo.modules.web.entity.WebEnquiryGoodsReply;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebEnquiryGoodsReplyService;
import com.yuhanginfo.modules.web.service.WebEnquiryGoodsService;
/**
 * 询价管理
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/enquire")
public class WebEnquireGoodsController  extends BaseController{
	@Autowired
	private WebEnquiryGoodsReplyService webEnquiryGoodsReplyService;
	@Autowired
	private WebEnquiryGoodsService webEnquiryGoodsService;
	
	@RequestMapping(value = "sendEnquireGoodsPrice")
	public String sendEnquireGoodsPrice(Model model){
		WebYongUser yongUser = WebUserUtils.getYongUser();
		//普通用户
		if("1".equals(yongUser.getUserType())){
			model.addAttribute("phone", yongUser.getMobile());
		}
		else{
			
			model.addAttribute("phone", yongUser.getLinkmanPhone());
			
		}
		model.addAttribute("actionCenter", "pr");
		return "modules/web/price/sendPrice";
	}

	@RequestMapping(value = "myEnquireList")
	public String myEnquireList(Model model,WebEnquiryGoods webEnquiryGoods,String status,String createDate1,String code){
		webEnquiryGoods.setUserId(WebUserUtils.getYongUser().getId());
		if(status!=null &&status!=""){
			webEnquiryGoods.setStatus(status);
			model.addAttribute("status", status);
		}
		
		if(code!=null && code!=""){
			webEnquiryGoods.setCode(code);
			model.addAttribute("code", code);
		}
		
		if(createDate1!=null&&createDate1!="" && !"询价日期筛选".equals(createDate1)&&createDate1.length()>0){
			String startDate=createDate1.substring(0, 10);
			String endDate=createDate1.substring(13, createDate1.length());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				webEnquiryGoods.setStartTime(sdf.parse(startDate +' '+"00:00:00"));
				webEnquiryGoods.setEndTime(sdf.parse(endDate +' '+"23:59:59"));
				model.addAttribute("createDate", createDate1);
			} catch (ParseException e) {
				logger.error(e.getMessage(),e);
			}
		}
		List<WebEnquiryGoods> list=webEnquiryGoodsService.selectByUserId(webEnquiryGoods);
		model.addAttribute("list", list);
		model.addAttribute("actionCenter", "price");
		return "modules/web/price/myPrice";
	}
	
	@RequestMapping(value = "sendPriceSave")
	public String sendPriceSave( @RequestParam("file") MultipartFile[] file,Model model,String[] partsCode,Integer[] num,String[] remarks,String[] receiveTime,String contacts, String phone,String oicq,String[] quality,String whetherTicket ) throws IOException{
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webIndex";
		}
		String[] fileName=new String[partsCode.length];
		   Integer gs=0;
		   StringBuilder buf=new StringBuilder();
			for (int i = 0; i < file.length; i++) {
				if(file[i].getSize()>0){
				FileUtils.saveSimpleNewImage(WebUserUtils.getYongUser().getId(), file[i].getOriginalFilename(), file[i].getInputStream());
				buf.append(FileUtils.saveSimpleNewImage(WebUserUtils.getYongUser().getId(), file[i].getOriginalFilename(), file[i].getInputStream())+";");
			 }else{	
				 fileName[gs]=buf.toString();
				 gs++;
				 buf=new StringBuilder();
			 }
			
			}
			
		  SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 
		  String dat=sdf.format(new Date());
		  List<WebEnquiryGoods> list=webEnquiryGoodsService.selectEnquiryList();
		  StringBuilder buss = new StringBuilder();
		  for (int i = 0; i < quality.length; i++) {
			if(quality[i]!=null && quality[i]!=""&&quality[i].length()>0 ){
				buss.append(quality[i]+";");
			}
		}
		  String qualitys=buss.toString();
		  for (int i = 0; i < partsCode.length; i++) {
		    try {
		          WebEnquiryGoods enquiryGoods=new WebEnquiryGoods();
		          SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		          
		          if(receiveTime!=null  && receiveTime.length>0){
						if(receiveTime[i]!=null&&receiveTime[i]!=""){
							enquiryGoods.setReceiveTime(sf.parse(receiveTime[i]));
						}else{
							enquiryGoods.setReceiveTime(null);
						}
					}else{
						enquiryGoods.setReceiveTime(null);
					}
		             
			      enquiryGoods.setPartsInfo(partsCode[i]);
			      enquiryGoods.setNum(num[i]);
			      
			      
			      if(remarks!=null  && remarks.length>0){
						if(remarks[i]!=null&&remarks[i]!=""){
							enquiryGoods.setRemarks(remarks[i]);
						}else{
							enquiryGoods.setRemarks(null);
						}
					}else{
						enquiryGoods.setRemarks(null);
					}
			      
			      
				  enquiryGoods.setContacts(contacts);
				  enquiryGoods.setPhone(phone);
				  enquiryGoods.setOicq(oicq);
				  enquiryGoods.setCreateDate(new Date()); 
				  enquiryGoods.setUpdateDate(new Date());
				  enquiryGoods.setQuality( qualitys);
				  enquiryGoods.setWhetherTicket(whetherTicket);
				  enquiryGoods.setStatus("1");
				  String codes=null;
				  Integer siz=list.size()+1;
				  if(siz<10){
					  codes=dat+"000"+siz;
				  }
				  if(siz>=10 && siz<100){
					  codes=dat+"00"+siz;
				  }
				  
				  if(siz>=100 && siz<1000){
					  codes=dat+"0"+siz;
				  }

				  if(siz>=1000 && siz<9999){
					  codes=dat+"0"+siz;
				  }
				  enquiryGoods.setCode(codes);
				  enquiryGoods.setUserId(WebUserUtils.getYongUser().getId());
				  enquiryGoods.setPhoto(fileName[i]);;
				  webEnquiryGoodsService.saveEnquiry(enquiryGoods);
		  } catch (ParseException e) {
			  logger.error(e.getMessage(),e);
		  }
		  
		  }
		  
		  
		model.addAttribute("actionCenter", "price");
		return "redirect:"+Global.getFrontPath()+"/enquire/myEnquireList";
	}
	
	//回复详情
	@RequestMapping(value = "receipeDetail")
	public String receipeDetail(String code ,Model model){
		List<WebEnquiryGoods> enquiryGoods=webEnquiryGoodsService.selectByCode(code);
		for (WebEnquiryGoods webEnquiryGoods : enquiryGoods) {
			String[] image=webEnquiryGoods.getPhoto().split(";");
			List<Object> list0=new ArrayList<Object>();
			for (int i = 0; i < image.length; i++) {
				if(image[i]!=null && image[i]!=""){
					list0.add(image[i]);
				}
			}
			webEnquiryGoods.setImages(list0);
		}
		
		List<WebEnquiryGoodsReply> list=new ArrayList<WebEnquiryGoodsReply>();
		List<WebEnquiryGoodsReply> list3=new ArrayList<WebEnquiryGoodsReply>();
		
		for (int i = 0; i < enquiryGoods.size(); i++) {
			WebEnquiryGoodsReply enquiry=new  WebEnquiryGoodsReply();
			WebEnquiryGoodsReply enquiryGoodsReply=new  WebEnquiryGoodsReply();
			List<WebEnquiryGoodsReply> webEnquiryGoodsReply=webEnquiryGoodsReplyService.selectByEnquiryGoodsId(enquiryGoods.get(i).getId());
			
			List<WebEnquiryGoodsReply> reply=new ArrayList<WebEnquiryGoodsReply>();
			if(!webEnquiryGoodsReply.isEmpty()){
				for (WebEnquiryGoodsReply webEnquiryGoodsReply2 : webEnquiryGoodsReply) {
					reply.add(webEnquiryGoodsReply2);
				}
				WebEnquiryGoods webEnquiry=webEnquiryGoodsService.selectById(enquiryGoods.get(i).getId());
				enquiry.setPartsCode(webEnquiry.getPartsInfo());
				enquiry.setCreateDate(webEnquiry.getCreateDate());
				
			}
			enquiryGoodsReply.setEnquiryGoodsReply(enquiry);
			enquiryGoodsReply.setWebEnquiryGoodsReplys(reply);
			list.add(enquiryGoodsReply);	
		}
		
		
		if(list!=null){
		for (WebEnquiryGoodsReply webEnquiryGoodsReply : list) {
			List<WebEnquiryGoodsReply> list1=webEnquiryGoodsReply.getWebEnquiryGoodsReplys();
			for (WebEnquiryGoodsReply webEnquiryGoodsReply2 : list1) {
				String[] image=webEnquiryGoodsReply2.getPhotos().split(";");
				List<Object> list0=new ArrayList<Object>();
				for (int i = 0; i < image.length; i++) {
					if(image[i]!=null && image[i]!=""){
						list0.add(image[i]);
					}
				}
				webEnquiryGoodsReply2.setImages(list0);
			}
			
		}
		}
		
		WebEnquiryGoods webEnquiryGoods =enquiryGoods.get(0);
		String[] quit=webEnquiryGoods.getQuality().split(";");
		StringBuilder buf=new StringBuilder();
		  for (int i = 0; i < quit.length; i++) {
			
				if("1".equals(quit[i])){
					buf.append("原厂"+"，");
				}
				if("2".equals(quit[i])){
					buf.append("国内品牌"+"，");
				}
				if("3".equals(quit[i])){
					buf.append("国外品牌"+"，");
				}
				if("4".equals(quit[i])){
					buf.append("其他"+"，");
				}
			
		}
		String qus= buf.toString();
		if(qus.length()>0) {
			model.addAttribute("qus",qus.substring(0, qus.length()-1));
		}
		model.addAttribute("list3", list3);
		model.addAttribute("code", code);
		model.addAttribute("list", list);
		model.addAttribute("enquiryGoods", webEnquiryGoods);
		model.addAttribute("enquiry", enquiryGoods);
		return "modules/web/price/enquireDetail";
	}
	
	
}
